package com.zm.devmag.device.service.Impl;


import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.zm.devmag.device.dao.CategoryMapper;
import com.zm.devmag.device.dao.InOutStockMapper;
import com.zm.devmag.device.dao.TempDataMapper;
import com.zm.devmag.device.dao.WarehouseMapper;
import com.zm.devmag.device.po.Category;
import com.zm.devmag.device.po.DeviceDetail;
import com.zm.devmag.device.po.InOutStock;
import com.zm.devmag.device.po.TempData;
import com.zm.devmag.device.po.Warehouse;
import com.zm.devmag.device.service.CategoryServer;
import com.zm.devmag.device.service.DeviceDetailServer;
import com.zm.devmag.user.po.Users;
import com.zm.devmag.user.server.UsersServer;
import com.zm.devmag.util.ExcelRead;


@Service
public class CategoryServerImpl implements CategoryServer{
	@Autowired
	private CategoryMapper catgMapper;
	@Autowired
	private TempDataMapper tdMapper;
	@Autowired
	private DeviceDetailServer ddsever;
	@Autowired
	private WarehouseMapper whMapper;
	@Autowired
	private UsersServer userService;
	@Override
	public List<Category> findAll() {
		// TODO Auto-generated method stub
		return catgMapper.findAll();
	}
	public int insertSelective(Category record){
		return catgMapper.insertSelective(record);
	}
	public String importDeviceByExcel(MultipartFile file,String uacc){
		List<ArrayList<String>> list =null;
		try {
			 list = new ExcelRead().readExcel(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(list.size()>0){
			/*for(ArrayList<String> arr:list){
				addCategoryTable(list.get(0));
			}*/
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	        String ino=sdf.format(new Date());
	        if(addTemDateFromExcel(list,ino,uacc)>0)
	        	return ino;
	        else
	        	return null;
		     	 
		}else{
			return null;
		}
	}
	public  int updateByPrimaryKeySelective(Category record){
		return catgMapper.updateByPrimaryKeySelective(record);
	}
	public Category selectByPrimaryKey(Long id){
		return catgMapper.selectByPrimaryKey(id);
	}
	public List<Category> getCategroyByCtype(String ctype){
		return catgMapper.getCategroyByCtype(ctype);
	}
	public List<TempData> getAllTempDataByINo(String ino){
		return tdMapper.selectByImportno(ino);
	}
	public int selectSubCataCountBySid(Long sid){
		return catgMapper.selectSubCataCountBySid(sid);
	}
	public int deleteByPrimaryKey(Long id){
		return catgMapper.deleteByPrimaryKey(id);
	}
	public List<Category> selectChildCateByPid(Long pid){
		return catgMapper.selectChildCateByPid(pid);
	}
	/*
	 * arr 读表数据 ino 数据id ,操作人账号
	 */
	private int addTemDateFromExcel(List<ArrayList<String>> arr,String ino,String uacc ){
		TempData tempcate =null;
		List<TempData> inlist = new ArrayList<TempData>();
		Users user = userService.SelectUserByUacc(uacc) ;
		for(ArrayList<String> ca:arr){
			if(ca.size()<9)continue;
			if(ca.get(0).equals("")&&ca.get(1).equals("")) continue;
			if(ddsever.checkIsHasByNO(ca.get(3))>0) continue;
			Warehouse wh = whMapper.selectByPartName(ca.get(9));
			tempcate = new TempData();
			if(wh == null){
				continue;
			}else{
				
				if(user.getUtype().equals("1")){
					tempcate.setWn(wh.getWhname());
					tempcate.setWnis("1");
				}else {
					if(wh.getId()==user.getWid()){
						tempcate.setWn(wh.getWhname());
						tempcate.setWnis("1");
					}else continue;
				}
			}
			tempcate.setDn(ca.get(0));
			/*Category categ= catgMapper.findCategroyByName(ca.get(0));
			if(categ !=null){
				if(categ.getCtype().equals("3")){tempcate.setDnis("1");}
				else{tempcate.setDnis("2");}
			}else{
				tempcate.setDnis("2");
			}*/
			tempcate.setBrand(ca.get(1));
			tempcate.setOldno(ca.get(2));
			tempcate.setNo(ca.get(3));
			tempcate.setXhno(ca.get(4));
			tempcate.setCcno(ca.get(5));
			tempcate.setTcrq(ca.get(6));
			if(ca.get(7)==null || ca.get(7).equals("")){
				tempcate.setZjnx(Long.parseLong("0"));
			}else{
				tempcate.setZjnx(Long.parseLong(ca.get(7)));
			}
			if(ca.get(8)==null || ca.get(8).equals("")){
				tempcate.setCgjg(0.0);
			}else{
				tempcate.setCgjg(Double.parseDouble(ca.get(8)));
			}
			
			tempcate.setStatus("正常");
			tempcate.setRemark("表格导入设备数据");
			tempcate.setImortno(ino);
			inlist.add(tempcate);
	}
		if(inlist.size()>0)
			return tdMapper.insertCategoryBatch(inlist);
		else
			return 0;
	}
	@Autowired
	private InOutStockMapper iosMapper;
	public Map<String, String> addCategoryTable(String importno,String cid,String fcid,String dcid,String sarr[],Long uid){
		Map<String, String> remap = new HashMap<String, String>();
		List<DeviceDetail>ddlist = new ArrayList<DeviceDetail>();
		List<InOutStock> inlist = new ArrayList<InOutStock>();
		Long maxid = ddsever.getMaxIdValue();
		if(maxid==null) maxid=(long)0 ;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(String sid : sarr){
			TempData td = tdMapper.selectById(Long.parseLong(sid));
			if(td!=null){
				DeviceDetail dd = new DeviceDetail();
				dd.setCid(Long.parseLong(cid));
				dd.setFcid(Long.parseLong(fcid));
				List<Category> ocatelist= catgMapper.findCategroyByName(td.getDn());
				if(ocatelist.size()>0){//设备列表中有同名的设备分类
					Category ishad =null;
					for(Category oc :ocatelist){
						if(! oc.getCtype().equals("3")) continue;
						if(oc.getPid()!=Long.parseLong(fcid))continue;
						if(oc.getPid()==Long.parseLong(fcid)){
							ishad =oc;
							break;
						}
					}
					if(ishad!=null){//有设备分类
						dd.setDcid(ishad.getId());
					}else{//添新设备分类
						Long cateid =catgMapper.getMaxIdValue();
						Category categ = new Category();
						categ.setId(cateid +1);
						categ.setPid(Long.parseLong(fcid));
						categ.setName(td.getDn());
						categ.setCtype("3");
						catgMapper.insertSelective(categ);
						dd.setDcid(cateid +1);
					}
					
				}else{//添新设备分类
					Long cateid =catgMapper.getMaxIdValue();
					Category categ = new Category();
					categ.setId(cateid +1);
					categ.setPid(Long.parseLong(fcid));
					categ.setName(td.getDn());
					categ.setCtype("3");
					catgMapper.insertSelective(categ);
					dd.setDcid(cateid +1);
				}
				if(td.getWnis().equals("2")){//新库名
					Long whid =whMapper.getMaxIdValue();
					Warehouse wh = new Warehouse();
					wh.setId(whid+1);
					wh.setWhname(td.getWn());
					whMapper.insertSelective(wh);
					dd.setWid(whid +1);
					dd.setOriwid(whid +1);
				}else{
					Warehouse wh= whMapper.selectWHByWhname(td.getWn());
					dd.setWid(wh.getId());
					dd.setOriwid(wh.getId());
				}
				dd.setBrand(td.getBrand());
				dd.setOldno(td.getOldno());
				dd.setNo(td.getNo());
				dd.setXhno(td.getXhno());
				dd.setCcno(td.getCcno());
				dd.setTcrq(td.getTcrq());
				dd.setZjnx(td.getZjnx());
				dd.setCgjg(td.getCgjg());
				dd.setStatus("1");	
				dd.setId(maxid +1 );
				dd.setRemark("导入设备数据，"+"时间："+sdf.format(new Date()));
				
				ddlist.add(dd);
				
				InOutStock ios = new InOutStock();//插入数据操作记录操作记录
				 ios.setDid(dd.getId());
				 ios.setCorm(Long.parseLong("1"));
				 ios.setOldpos(dd.getWid());
				 ios.setPos(dd.getWid());
				 
				 ios.setCotime(sdf.format(new Date()));
				 ios.setCouser(uid);
				 inlist.add(ios);
				maxid+=1;
			}
		}
		if(ddsever.insertDeviceDetailBatch(ddlist)>0 && iosMapper.insertInOutStockBatch(inlist)>0){
			remap.put("ok", "ok");
		}
		return remap;
	}
}
